package com.neiquan.backstage.myapply.web.action;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neiquan.backstage.common.util.CommonResp;
import com.neiquan.backstage.common.util.ExtraSpringHibernateTemplate;
import com.neiquan.backstage.common.util.Sessions;
import com.neiquan.backstage.meeting_order.MeetingOrderService;
import com.neiquan.backstage.pub.bsc.dao.po.Lease;
import com.neiquan.backstage.pub.bsc.dao.po.MeetingOrder;
import com.neiquan.backstage.pub.bsc.dao.po.SceneVisitReservation;
import com.neiquan.backstage.pub.bsc.dao.po.User;
import com.neiquan.backstage.resourec_manage.ResourecLeaseService;
import com.neiquan.backstage.scene_visit_reservation.SceneVisitReservationService;


@Controller
@RequestMapping(value = "myApplyAction", produces = { "text/json;charset=UTF-8" })
public class MyApplyAction {
	private static Class<?> pocR = MeetingOrder.class;
	private static Class<?> pocS = SceneVisitReservation.class;
	private static Class<?> pocL = Lease.class;
	@Autowired
	private ExtraSpringHibernateTemplate extraSpringHibernateTemplate;
	
	@Autowired
	private ResourecLeaseService resourecLeaseService;
	
	@Autowired
	private SceneVisitReservationService sceneVisitReservationService;
	
	@Autowired
	private MeetingOrderService meetingOrderService;
	
	@ResponseBody
	@RequestMapping("list")
	public String list(HttpSession session) {
		User loginedUser = (User) Sessions.getLoginedUser(session);
		Integer memberId = loginedUser.getMemberId();
		Integer userId = loginedUser.getUserId();
		List<Lease> entityLs = resourecLeaseService.queryByUserId(userId, memberId);
		List<SceneVisitReservation> entitySs = sceneVisitReservationService.queryByUserId(userId, memberId);
		List<MeetingOrder> entityMs = meetingOrderService.queryByUserId(userId, memberId);
		List<Object> list = new ArrayList<>();
		for(Lease l:entityLs){
			list.add(l);
		}
		for(SceneVisitReservation s:entitySs){
			list.add(s);
				}
		for(MeetingOrder m:entityMs){
			list.add(m);
		}
		Collections.sort(list, new Comparator<Object>() {
			@Override
			public int compare(Object o1, Object o2) {
				return (int) (getTime(o1) - getTime(o2));
			}
		});
		System.out.println(CommonResp.buildSuccessResp(list).toJsonString());
		return CommonResp.buildSuccessResp(list).toJsonString();
	}
	
	
		private static Long getTime(Object o) {
			if (o instanceof Lease) {
				Lease a = (Lease)o;
				return a.getDtCreat();
			}
			if (o instanceof SceneVisitReservation) {
				SceneVisitReservation b = (SceneVisitReservation)o;
				return b.getDtCreat();
			}
			if (o instanceof MeetingOrder) {
				MeetingOrder c = (MeetingOrder)o;
				return c.getDtCreat();
			}
			return null;
			
		}

}
